package hk.ust.comp3111.ustnutrition;

import hk.ust.comp3111.ustnutrition.data.ApiHandler;
import hk.ust.comp3111.ustnutrition.data.DataAccessor;
import hk.ust.comp3111.ustnutrition.data.db.Food;
import hk.ust.comp3111.ustnutrition.data.db.IntakeRecord;
import hk.ust.comp3111.ustnutrition.data.json.ApiResponseBase;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RatingBar;
import android.widget.Toast;

public class IntakeRecordDialog extends DialogFragment
{
	public static IntakeRecordDialog createInstance(String foodId)
	{
		IntakeRecordDialog d = new IntakeRecordDialog();
		Bundle args = new Bundle();
		args.putString("foodId", foodId);
		d.setArguments(args);
		return d;
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mFood = DataAccessor.Instance.getFoods().get(
				getArguments().getString("foodId"));
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		ContextThemeWrapper theme = new ContextThemeWrapper(getActivity(),
				R.style.Theme_AppCompat_Light);
		LayoutInflater inflater = LayoutInflater.from(getActivity())
				.cloneInContext(theme);
		final View dialogView = inflater.inflate(R.layout.intake_record_dialog, null);
		initView(dialogView);
		OnClickListener recordListener = new OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						IntakeRecord record = new IntakeRecord(mFood);
						DataAccessor.Instance.getRecords().add(record);
						record.setChannel(DataAccessor.Instance.getUser().getITSC());
					}
				};
		OnClickListener rateListener = new OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						DataAccessor.Instance.rate(mFood, mRatingBar.getProgress(),
								new ApiHandler<ApiResponseBase>()
								{
									@Override
									public void onSuccess(ApiResponseBase response)
									{
										Toast.makeText(dialogView.getContext(),
												response.getMessage(),
												Toast.LENGTH_LONG).show();
									}

									@Override
									public void onFailure(String message)
									{
										Toast.makeText(dialogView.getContext(),
												message, Toast.LENGTH_LONG).show();
									}
								});
					}
				};

		AlertDialog.Builder builder = new AlertDialog.Builder(theme);
		Dialog d = builder
				.setView(dialogView)
				.setPositiveButton(R.string.food_dialog_add_record,
						recordListener)
				.setNeutralButton(R.string.food_dialog_rate,
						rateListener)
				.setNegativeButton(android.R.string.cancel, null)
				.create();
		return d;
	}

	@Override
	public void onDestroy()
	{
		if (mTask != null)
		{
			mTask.cancel(true);
			mTask = null;
		}
		super.onDestroy();
	}

	private void initView(View root)
	{
		mFoodView = (FoodListCardView)root.findViewById(R.id.food_info);
		mFoodView.setFood(mFood);
		mRatingBar = (RatingBar)root.findViewById(R.id.my_score);
	}

	private Food mFood;

	private FoodListCardView mFoodView;
	private RatingBar mRatingBar;

	private DownloadImageTask mTask;
}


